package com.hysf.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyAdviceInvocationHandler implements InvocationHandler {
	
	private final Object target; // 目标对象

	public MyAdviceInvocationHandler(Object target) {
		this.target = target;
	}
	
	public Object getProxy() {
		return Proxy.newProxyInstance(
				MyAdviceInvocationHandler.class.getClassLoader(),
				target.getClass().getInterfaces(),
				this
		);
	}
	
	public void checkPrivilege() {
		System.out.println("权限校验");
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if (method.getName().equals("addOrder")) {
			checkPrivilege();
		}
		return method.invoke(target, args);
	}
}
